import 'reflect-metadata'
import { Type } from 'class-transformer'
import { ArrayMinSize, IsArray, IsIn, IsIP, IsNotEmpty, IsString, Matches, MinLength, ValidateIf, ValidateNested } from 'class-validator'

export class choicesDTO {
  @IsNotEmpty({ message: '不能为空' })
  @IsString({ message: '必须是字符串' })
  name!: string;

  @IsNotEmpty({ message: '不能为空' })
  @IsString({ message: '必须是字符串' })
  value!: string;
}

export class deployListDTO {
  @IsNotEmpty({ message: '不能为空' })
  @IsString({ message: '必须是字符串' })
  VALUE!: string;

  @ValidateIf(({ RUN_MODEL }) => {
    return [3, 4, '3', '4'].includes(RUN_MODEL)
  })
  @IsIP(4, { message: '必须为ip地址' })
  SERVER_PATH?: string;

  @ValidateIf((allParam) => {
    return [3, 4, '3', '4'].includes(allParam.RUN_MODEL)
  })
  @IsNotEmpty({ message: '不能为空' })
  @IsString({ message: '必须是字符串' })
  SSH_USER?: string;

  @ValidateIf((allParam) => {
    return [3, 4, '3', '4'].includes(allParam.RUN_MODEL)
  })
  @IsNotEmpty({ message: '不能为空' })
  @IsString({ message: '必须是字符串' })
  PASSWORD?: string;

  @ValidateIf((allParam) => {
    return [3, 4, '3', '4'].includes(allParam.RUN_MODEL)
  })
  @Matches(/^\/(?:[^/]+\/)*[^/]+$/, { message: '路径必须符合Linux规则' })
  PATH?: string;

  @ValidateIf((allParam) => {
    return [4, '4'].includes(allParam.RUN_MODEL)
  })
  @IsNotEmpty({ message: '不能为空' })
  @IsString({ message: '必须是字符串' })
  SCRIPT?: string;

  @ValidateIf((allParam) => {
    return [4, '4'].includes(allParam.RUN_MODEL)
  })
  @IsNotEmpty({ message: '不能为空' })
  @IsString({ message: '必须是字符串' })
  ASSETS_PATH?: string;

  SERVER_CMD?: string | string[];

  @IsIn([1, 2, 3, 4, '1', '2', '3', '4'], { message: `必须是${[1, 2, 3, 4, '1', '2', '3', '4'].join(',')}里面其中一个值` })
  RUN_MODEL: string | number = 3;
}

export class Params {
  @IsNotEmpty({ message: '不能为空' })
  @IsString({ message: '必须是字符串' })
  name!: string;

  @IsArray({ message: '必须是一个数组' })
  @ArrayMinSize(1, { message: '至少要有一个' })
  @ValidateNested({ each: true })
  @Type(() => choicesDTO)
  choices!: choicesDTO[];

  @IsArray({ message: '必须是一个数组' })
  @ArrayMinSize(1, { message: '至少要有一个' })
  @ValidateNested({ each: true })
  @Type(() => deployListDTO)
  deployList!: deployListDTO[];
}
